﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EF;
using BL;
using BL.Entities;
using ApriBox.Mvc.UploadHelpers;

namespace ApriBox.Mvc.Controllers
{
    public class CreateController : Controller
    {
        //
        // GET: /Create/

        FSEntitiesRepository repository = new FSEntitiesRepository();

        [HttpPost]
        public ActionResult CreateFolder(int parentFolderId, string name)
        {
            if (name == "")
                name = "New Folder";

            IEnumerable<Folder> folders = repository.GetFolders(parentFolderId);

            foreach (Folder fol in folders)
            {
                if (fol.Name == name)
                    name = Name.ChangeName(name);
            }

            Folder f = new Folder
            {
                Name = name,
                PhysicalPath = "Virtual",
                ParentId = parentFolderId,
                DateCreate = DateTime.Now.Date,
                DateUpdate = DateTime.Now.Date,
                Owner = HttpContext.User.Identity.Name,
            };

            repository.CreateFolder(f);

            return Json(new { Id = parentFolderId });
        }

    }
}
